<?php 
## include required files
/*******************************/
require_once '../model/leads.php';
require_once '../model/state.php';
require_once '../model/trade.php';
require_once '../model/budget.php';
require_once '../model/membershipplan.php';
/*******************************/
## Create Objects
/*******************************/
$stateObj = new Model_State();
$leadsObj = new Model_Leads();
$tradeObj = new Model_TRADE();
$budgetObj = new Model_Budget();
$objPlan = new Model_PLAN();

/*******************************/

if(isset($_POST['addleads_btn'])) {	
	## apply PHP validation for required filed
	extract($_POST);
	if(trim($_POST['budget']) != '' && trim($_POST['price']) != '') {
		
		
		$leadsAddArray['budget']   = $_POST['budget'];		
		$leadsAddArray['jobtype']   = $_POST['jobtypeid'];		
		$leadsAddArray['region_id']      = $_POST['regionid'];
		//$leadsAddArray['suburb_id']      = $_POST['suburbid'];
		//$leadsAddArray['postalcode']      = $_POST['postalcode'];
		$leadsAddArray['price']      = $_POST['price'];
		$leadsAddArray['plan_id']   = $_POST['plan_id'];		
		$leadsAddArray['status']      = '1';
		$leadsAddArray['dateadded']      = date('Y-d-m H:i:s');
		
		$getleadsdetail = $leadsObj->getLeadsExits($_POST['regionid'],$_POST['jobtypeid'],$_POST['budget'],$_POST['price'],$_POST['postalcode']);
		if($getleadsdetail['lead_id']=='')
		{
			$leadsObj->addLeads($leadsAddArray);
			$_SESSION['msg'] = "<div class='success_msg'><span>Lead added successfully</span></div>";	
			header('location:'.SITE_URL.'/admin/home.php?q=leads&page='.$_POST['page']);
			exit;

		}
		else
		{
				$_SESSION['msg'] = "<div class='success_msg'><span>Lead already exits</span></div>";		
		}
		
	} else {// php validation else
		$_SESSION['msg'] = '<div class="error_msg"><span>Please fill all required fields</span></div>';
	}
	// main if closed
}

## Fetch all active states
$stateArray = $stateObj->getAllStateWithActiveStatus();
$smarty->assign('stateArray', $stateArray);

## Fetch all active suburb
if($_POST['regionid']!='')
{
	$stateid = $_POST['regionid'];
	$getstatedetail = $stateObj->getStateDetailsById($stateid);
	
	$suburbArray = $stateObj->getAllSuburb($getstatedetail['iso1_code']);
	//echo"<pre>";print_R($suburbArray);die();
	$smarty->assign('suburbArray', $suburbArray);
}


## Fetch all active job type
$jobtypeArray = $tradeObj->getAllJobTypeWithActiveStatus();
$smarty->assign('jobtypeArray', $jobtypeArray);

## Fetch all active budget
$budgetArray = $budgetObj->getAllBudgetWithActiveStatus();
$smarty->assign('budgetArray', $budgetArray);

$getplandetail = $objPlan->getAllPlan();
$smarty->assign("planArray", $getplandetail);



## Assign success or error msg to smarty variable and unset session variable
if(trim($_SESSION['msg']) != '') {
	$smarty->assign('msg', $_SESSION['msg']);
	unset($_SESSION['msg']);
}

## Set active class variable for left menu
$smarty->assign('activeclass', 'leads');
$smarty->assign('mainmenu', '2');
## Unset all the objects created which are on this page
unset($countryObj);
unset($leadsObj);

$smarty->display(TEMPLATEDIR_ADMIN. 'manageleads/addlead.tpl');
unset($smarty);
?>